import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import refresh from '@dfeidao/atom-web/url/refresh';
import { productid } from '../../atom/config';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 题库类型
	const question_bank_type=get(fd,'newArr');
	
	// 题目类型
	const topic_type="判断题";
	const re = await nodejs<{dt: Array<{topic: string}>}>('question-bank/zj-000004/s002',{
		filter:{
			question_bank_type,
			topic_type
		}
	});
	
	// 题目内容
	const topic1=fd.data.node.querySelector<HTMLInputElement>('[data-id="topic"]');
	const topic=topic1.value;
	for (let i=0;i < re.dt.length;i++) {
	 	if (topic === re.dt[i].topic) {
			 error("这道题已存在");
			 return;
		 }
	}
	// 正确内容
	const Y=fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="Y"]:checked');
	const N=fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="N"]:checked');
	let right_answer="";
	if (Y.length === 0 && N.length === 0) {
		error("请选择正确答案");
		return;
	} else if (Y.length > 0 && N.length === 0) {
		right_answer = "Y";
	} else if (Y.length === 0 && N.length > 0) {
		right_answer = "N";
	}
	const res = await nodejs<{code: 1|0;result: string}>('question-bank/zj-000004/s001',{
		filter:{
			productid,
			topic_type,
			topic,
			right_answer,
			question_bank_type
		}

	});
	if (res.code === 1) {
		alert("添加成功");
		refresh();

		} else {
			alert("添加失败");
		}
}
